/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import mint.mvc.annotation.BaseMapping;
import mint.mvc.annotation.Mapping;
import mint.mvc.renderer.TemplateRenderer;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Baojia;
import pojo.CarCountry;
import pojo.CarPailiang;
import pojo.CarPinpai;
import pojo.CarType;
import pojo.User;
import util.HibernateUtil;
import util.JSONRenderer;

@BaseMapping("/car")
public class CarAction {

    /**
     * "/index" 和 "/"都可以访问到此方法
     *
     * @return
     */
    @Mapping(urls = {"/add_country"}, method = "post")
    public String index(String countrynameString, HttpServletRequest hs) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        CarCountry cc = new CarCountry();
        cc.setCountryname(countrynameString);
        ss.save(cc);
        ss.close();
        return "添加成功，<a href=../baojia.jsp>返回</a>";
    }
     @Mapping(urls = {"/get_country"}, method = "get")
    public JSONRenderer get_country(String countrynameString,HttpServletRequest hs) {
       
        Session ss = HibernateUtil.getSessionFactory().openSession();
        String hql = "from CarCountry";
        Query query = ss.createQuery(hql);
        List<User> list = query.list();
        ss.close();
        return new JSONRenderer(list);
    }

    @Mapping(urls = {"/add_pinpai"}, method = "post")
    public String pinpai(String pinpai, Integer country_id, HttpServletRequest hs) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        CarPinpai cp = new CarPinpai();
        cp.setPinpai(pinpai);
        cp.setCountryId(country_id);
        ss.save(cp);
        ss.close();
        return "添加成功，<a href=../baojia.jsp>返回</a>";
    }

    @Mapping(urls = {"/add_cartype"}, method = "post")
    public String cartype(String cartype, Integer cartype_id, String baojia_URL_input) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        CarType ct = new CarType();
        ct.setCarType(cartype);
        ct.setPinpaiId(cartype_id);
        ct.setBaojiaUrlInput(baojia_URL_input);
//        cp.setPinpai(pinpai);
//        cp.setCountryId(country_id);
        ss.save(ct);
        ss.close();
        return "添加成功，<a href=../baojia.jsp>返回</a>";
    }
    @Mapping(urls = {"/add_pailiang"}, method = "post")
    public String add_pailiang(String pailiang, Integer pailiang_id_input, HttpServletRequest hs) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        CarPailiang ct = new CarPailiang();
        ct.setFatherId(pailiang_id_input);
        ct.setValueInput(pailiang);  
//        cp.setPinpai(pinpai);
//        cp.setCountryId(country_id);
        ss.save(ct);
        ss.close();
        return pailiang_id_input+"添加成功，<a href=../baojia.jsp>返回</a>";
    }

    @Mapping(urls = {"/get_pinpai"}, method = "get")
    public JSONRenderer getpinpai(String country_id, HttpServletRequest hs) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        String hql = "from CarPinpai where  country_id = :n ";
        Query query = ss.createQuery(hql);
        query.setString("n", country_id);
        List<User> list = query.list();
        ss.close();
        return new JSONRenderer(list);
    }

    @Mapping(urls = {"/get_cartype"}, method = "get")
    public JSONRenderer get_cartype(String farther_id, HttpServletRequest hs) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        String hql = "from CarType where  pinpai_id = :n ";
        Query query = ss.createQuery(hql);
        query.setString("n", farther_id);
        List<User> list = query.list();
        ss.close();
        return new JSONRenderer(list);
    }
    @Mapping(urls = {"/get_pailiang"}, method = "get")
    public JSONRenderer get_pailiang(String farther_id, HttpServletRequest hs) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        String hql = "from CarPailiang where  fatherId = :n ";
        Query query = ss.createQuery(hql);
        query.setString("n", farther_id);
        List<User> list = query.list();
        ss.close();
        return new JSONRenderer(list);
    }
    @Mapping(urls = {"/get_baojia"}, method = "get")
    public JSONRenderer get_baojia(String country_name, String pinpai_name, String xilie_name, String pailiang_name, 
            String nianfen_name ) {
        System.out.println(country_name+ pinpai_name+ xilie_name+ pailiang_name+ nianfen_name);
        Session ss = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Baojia where country_name = :n and pinpai_name = :n2 and xilie_name = :n3 and"
                + " pailiang_name = :n4 and nianfen_name = :n5";
        Query query = ss.createQuery(hql);
        query.setString("n", country_name);
        query.setString("n2", pinpai_name);
        query.setString("n3", xilie_name);
        query.setString("n4", pailiang_name);
        query.setString("n5", nianfen_name);
        List<User> list = query.list();
        ss.close();
        return new JSONRenderer(list);
    }
    @Mapping(urls = {"/add_baojia"}, method = "get")
    public String add_baojia(String country_name, String pinpai_name, String xilie_name, String pailiang_name, 
            String nianfen_name,Double baojia,String url) {
        System.out.println(country_name+ pinpai_name+ xilie_name+ pailiang_name+ nianfen_name);
        Session ss = HibernateUtil.getSessionFactory().openSession();
        Baojia cc = new Baojia();
        cc.setCountryName(country_name);
        cc.setPinpaiName(pinpai_name);
        cc.setXilieName(xilie_name);
        cc.setPailiangName(pailiang_name);
        cc.setNianfenName(nianfen_name);
        cc.setBaojia(baojia);
        cc.setUrl(url);
        ss.save(cc);
        ss.close();
        return "添加成功，<a href=../baojia.jsp>返回</a>";
    }
    
    
    @Mapping(urls = {"/country_del"}, method = "get")
    public JSONRenderer country_del(Integer country_id) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.beginTransaction();

        CarCountry iid = (CarCountry) ss.get(CarCountry.class, country_id);
        ss.delete(iid);
        
        ss.getTransaction().commit();
        ss.close();
        
        return new JSONRenderer(iid);
    }
    @Mapping(urls = {"/cartype_del"}, method = "get")
    public JSONRenderer cartype_del(Integer id) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.beginTransaction();

        CarType iid = (CarType) ss.get(CarType.class, id);
        ss.delete(iid);
        
        ss.getTransaction().commit();
        ss.close();
        
        return new JSONRenderer(iid);
    }
    @Mapping(urls = {"/pailiang_del"}, method = "get")
    public JSONRenderer pailiang_del(Integer id) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.beginTransaction();

        CarPailiang iid = (CarPailiang) ss.get(CarPailiang.class, id);
        ss.delete(iid);
        
        ss.getTransaction().commit();
        ss.close();
        
        return new JSONRenderer(iid);
    }
    @Mapping(urls = {"/pinpai_del"}, method = "get")
    public JSONRenderer pinpai_del(Integer id) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.beginTransaction();

        CarPinpai iid = (CarPinpai) ss.get(CarPinpai.class, id);
        ss.delete(iid);
        
        ss.getTransaction().commit();
        ss.close();
        
        return new JSONRenderer(iid);
    }
    @Mapping(urls = {"/country_edit"}, method = "get")
    public JSONRenderer country_edit(Integer country_id,String countryname) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.beginTransaction();

        CarCountry iid = (CarCountry) ss.get(CarCountry.class, country_id);
        iid.setCountryname(countryname);
        ss.update(iid);
        
        ss.getTransaction().commit();
        ss.close();
        
        return new JSONRenderer(iid);
    }
    @Mapping(urls = {"/pinpai_edit"}, method = "get")
    public JSONRenderer pinpai_edit(Integer id,String countryname) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.beginTransaction();

        CarPinpai iid = (CarPinpai) ss.get(CarPinpai.class, id);
        iid.setPinpai(countryname);
        ss.update(iid);
        
        ss.getTransaction().commit();
        ss.close();
        
        return new JSONRenderer(iid);
    }
    @Mapping(urls = {"/cartype_edit"}, method = "get")
    public JSONRenderer cartype_edit(String cartype, Integer id, String baojia_URL_input) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.beginTransaction();

        CarType iid = (CarType) ss.get(CarType.class, id);
        iid.setCarType(cartype);
        iid.setBaojiaUrlInput(baojia_URL_input);
        ss.update(iid);
        
        ss.getTransaction().commit();
        ss.close();
        
        return new JSONRenderer(iid);
    }
    
}
